

import Common from '../actions/common';

import {getQueryCondition} from '../../../../hrpub/common/utils/utils';


export default class TableAction extends Common{
    constructor(comp) {
        super();
        this.comp = comp;
    }

    didMount = () => {
        this.subscribe();
    }

    // 取消订阅
    willUnMount = () => {
        this.pubSub.unSubscribe('getMainTable');
    }

    // 订阅事件
    subscribe = () => {
        this.pubSub.subscribe('getMainTable', (...args) => {
            this.getTableData(...args);
        });
    }
    
    // 获取表格数据
    getTableData = async ({type, conditions, oid, pageInfo, pk_org} = {}) => {
        const {
            props: {
                dispatch,
                sca,
                search,
                meta,
                editTable
            }
        } = this.comp;

        if(type === undefined) {
            type = sca.page;
        }

        let template = meta.getMeta();
        let searchId = this.searchMap[type];

        if(conditions === undefined) {
            conditions = getQueryCondition(this.comp.props, searchId);
        }
        if(oid === undefined) {
            oid = template[searchId].oid;
        }
        if(pageInfo === undefined) {
            pageInfo = sca.pageInfo;
        }
        if(pk_org === undefined) {
            pk_org = sca.orgValue[type].refpk;
        }

        pageInfo = {
            ...pageInfo,
            pageIndex: pageInfo.pageIndex
        };

        let postData = {
            ...conditions,
            oid: oid,
            pageInfo: pageInfo,
            pk_org: pk_org,
            queryType: this.listMap[type]
        };

        try {
            let res = await this.getMainTableData({
                postData: postData
            });

            if(res.success) {
                if(res.data) {
                    this.update({
                        allPks: res.data[this.listMap[type]].allpks
                    });
                    this.setTableData(res.data);
                }
                else {
                    this.setTableData({
                        [this.listMap[type]]: {
                            rows: [],
                            pageInfo: {
                                pageIndex: 1,
                                pageSize: 10,
                                total: 0,
                                totalPage: 1
                            }
                        }
                    });
                }
            }
            
        }
        catch(e) {
            console.error(`getTableData: ${e}`);
        }

    }

    // 设置表格数据
    setTableData = (data) => {
        const {
            props: {
                sca,
                dispatch,
                editTable
            }
        } = this.comp;

        let tableId = this.listMap[sca.page];
        let tableData = data[tableId];
        let pageInfo = this.deepCopy(tableData.pageInfo);

        editTable.setTableData(tableId, tableData, false);

        dispatch({
            type: 'sca/handleParamsByKey',
            payload: {
                pageInfo: {
                    ...pageInfo,
                    pageIndex: parseInt(pageInfo.pageIndex)
                }
            }
        });
    }
}